﻿using MVC_Project.BLL;
using Project.DAL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Project.BLL
{
    public class UsersBll: BaseBll<Users>
    {
        UsersDal dal = new UsersDal();
        public override void SetDal()
        {
            Dal = new UsersDal();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Users> GetUsersByPage(string name, int page, int size, out int count) {
            var users = base.Query();

            if (!string.IsNullOrWhiteSpace(name))
            {
                users = users.Where(u => u.UName.Contains(name));
            }
            count = users.Count();

            var data = users.OrderBy(u=>u.UId).Skip((page-1)*size).Take(size);

            return data.ToList();
        }


        public Users GetUsersByID(int UId) {
            return base.Query().Where(u => u.UId == UId).FirstOrDefault();
        }

        public bool ValidPhone(string phone) {
            int num = base.Query().Where(u => u.Phone == phone).Count();
            return num == 0;
        }

        public Users Login(string phone,string pwd) {
            return base.Query().Where(u => u.Phone==phone && u.loginpwd == pwd).FirstOrDefault();
        }

        public bool UpdateUser(Users users) { 
            var user = Dal.Query().Where(u=>u.UId ==users.UId).FirstOrDefault();
            if (user != null) { 
                user.UName=users.UName;
                user.Phone=users.Phone;
                user.loginpwd = users.loginpwd;
                user.Sex=users.Sex;
                //user.birthday=users.birthday;
                user.Status=users.Status;
                user.RId=users.RId;

                Dal.Update(user);

                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteUser(int UId)
        {
            var user = Dal.Query().Where(u => u.UId == UId).FirstOrDefault();
            if (user != null)
            {
                Dal.Delete(user);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteUser(int[] UId)
        {
            var users = Dal.Query().Where(u => UId.Contains(u.UId)).ToList();
            if (users != null)
            {
                Dal.Delete(users);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public Users GetUsersOne(int Uid) {
            var user = Dal.Query().Where(u=>u.UId==Uid).FirstOrDefault();
            return user;
        }

        //判断旧密码是否一致
        public bool EditOldPwd(int uid,string pwd) { 
            Users data = Dal.Query().Where(u => u.UId == uid).FirstOrDefault();

            if (data.loginpwd == pwd)
            {
                return true;
            }
            else { 
                return false;
            }
        }

        public bool EditNewPwd(int uid, string pwd) { 
            return dal.EditNewPwd(uid, pwd);
        }

    }
}
